/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.poll.town.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.common.core.util.CollectionUtils;
import com.hopes.bizz.poll.common.constant.OmConstant;
import com.hopes.bizz.poll.common.constant.PollFactorConstant;
import com.hopes.bizz.poll.information.entity.PollTownEntity;
import com.hopes.bizz.poll.information.service.PollTownService;
import com.hopes.bizz.poll.om.vo.AQIData;
import com.hopes.bizz.poll.om.vo.AQISort;
import com.hopes.bizz.poll.town.entity.PollTownRtDataEntity;
import com.hopes.bizz.poll.town.mapper.PollTownRtDataMapper;
import com.hopes.bizz.poll.town.service.PollTownRtDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ${tableComment}
 *
 * @author pig
 * @date 2023-09-26 18:58:34
 */
@Service
public class PollTownRtDataServiceImpl extends ServiceImpl<PollTownRtDataMapper, PollTownRtDataEntity> implements PollTownRtDataService {

    @Autowired
    private PollTownService pollTownService;

    @Override
    public AQISort getAqiSort() {
        ArrayList<String> name = new ArrayList<>();
        ArrayList<Double> PM2_5List = new ArrayList<>();
        ArrayList<Double> PM10List = new ArrayList<>();
        ArrayList<Double> COList = new ArrayList<>();
        ArrayList<Double> NO2List = new ArrayList<>();
        ArrayList<Double> O3List = new ArrayList<>();
        ArrayList<Double> SO2List = new ArrayList<>();
        ArrayList<Double> AQIList = new ArrayList<>();
        //获取县镇AQI
        List<Long> townIdList = pollTownService.lambdaQuery().eq(PollTownEntity::getUseFlag, Boolean.TRUE).list().stream().map(PollTownEntity::getId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(townIdList)){
            return null;
        }
        List<PollTownRtDataEntity> pollTownRtDataEntities = baseMapper.selectList(Wrappers.lambdaQuery(PollTownRtDataEntity.class).
                in(PollTownRtDataEntity::getTownId, townIdList)
                .eq(PollTownRtDataEntity::getDataType, OmConstant.DATA_TYPE_RT)).stream()
                .filter(item-> ObjectUtil.isNotNull(item.getAqiData()))
                .sorted(Comparator.comparing(PollTownRtDataEntity::getAqiData).reversed()).collect(Collectors.toList());

        // 计算平均值
        double PM2_5, PM10, CO, NO2, O3, SO2, AQI;
        for (PollTownRtDataEntity pollRtDataEntity : pollTownRtDataEntities) {
            name.add(pollRtDataEntity.getTownIdStr());
            JSONObject dataJson = pollRtDataEntity.getDataJson();
            if (dataJson.containsKey(PollFactorConstant.PM2_5)) {
                PM2_5 = dataJson.getJSONObject(PollFactorConstant.PM2_5).getDouble(OmConstant.VAL);
                PM2_5List.add(PM2_5);
            }
            if (dataJson.containsKey(PollFactorConstant.PM10)) {
                PM10 = dataJson.getJSONObject(PollFactorConstant.PM10).getDouble(OmConstant.VAL);
                PM10List.add(PM10);
            }
            if (dataJson.containsKey(PollFactorConstant.CO)) {
                CO = dataJson.getJSONObject(PollFactorConstant.CO).getDouble(OmConstant.VAL);
                COList.add(CO);
            }
            if (dataJson.containsKey(PollFactorConstant.NO2)) {
                NO2 = dataJson.getJSONObject(PollFactorConstant.NO2).getDouble(OmConstant.VAL);
                NO2List.add(NO2);
            }
            if (dataJson.containsKey(PollFactorConstant.O3)) {
                O3 = dataJson.getJSONObject(PollFactorConstant.O3).getDouble(OmConstant.VAL);
                O3List.add(O3);
            }
            if (dataJson.containsKey(PollFactorConstant.SO2)) {
                SO2 = dataJson.getJSONObject(PollFactorConstant.SO2).getDouble(OmConstant.VAL);
                SO2List.add(SO2);
            }
            AQI = pollRtDataEntity.getAqiData();
            AQIList.add(AQI);
        }
        AQIData aqiData = new AQIData();
        aqiData.setPM2_5(PM2_5List);
        aqiData.setPM10(PM10List);
        aqiData.setCO(COList);
        aqiData.setNO2(NO2List);
        aqiData.setO3(O3List);
        aqiData.setSO2(SO2List);
        aqiData.setAQI(AQIList);
        AQISort aqiSort = new AQISort();
        aqiSort.setTownName(name);
        aqiSort.setData(aqiData);
        return aqiSort;
    }
    @Override
    public void deleteByTownIdAndDataType(List<Long> townIdList, Integer dataType) {
        QueryWrapper<PollTownRtDataEntity> wrapper = new QueryWrapper<>();
        wrapper.in("town_id", townIdList);
        wrapper.eq("data_type", dataType);
        getBaseMapper().delete(wrapper);
    }
}
